package com.way.fileexplorer.util;

import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.way.fileexplorer.R;
import com.way.fileexplorer.activities.FileExploreActivity;

public class NotifyManager {
	private static final int TOAST_LONG_DELAY = 3500;
	private static final int TOAST_SHORT_DELAY = 2000;

	private static Context mContext;
	private static NotifyManager mManager;
	private static NotificationManager mNotifyManager;
	private Toast mToast;
	private AlertDialog mExitDialog;
	private AlertDialog mDialog;
	private Handler mMainThreadHandler;
	private static String TAG = "NotifyManager";

	private NotifyManager(Context context) {
		mContext = context;
		mNotifyManager = (NotificationManager) mContext
				.getSystemService(Context.NOTIFICATION_SERVICE);
		mMainThreadHandler = new Handler(mContext.getMainLooper());
		createDialogs();

	}

	public static void init(Context context) {
		if (context == null) {
			Slog.e("context == null");
			throw new NullPointerException();
		}
		mManager = new NotifyManager(context);
	}

	public static NotifyManager getInstance(Context context) {
		if (mManager == null || mContext == null) {
			Log.e(TAG, "Should not use this,before init");
			init(context);
		}
		return mManager;
	}

	private Runnable cancelToast = new Runnable() {
		@Override
		public void run() {
			mToast = null;
		}
	};

	private void createDialogs() {
		mExitDialog = new AlertDialog.Builder(mContext)
				.setTitle(
						mContext.getString(R.string.text_exit_file_explore_title))
				.setMessage(R.string.text_exit_file_explore_prompt)
				.setNegativeButton(
						mContext.getString(R.string.common_text_cancel),
						new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub

							}
						})
				.setPositiveButton(mContext.getString(R.string.common_text_ok),
						new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								((FileExploreActivity) mContext).finish();

							}
						}).create();
	}

	public AlertDialog getExitDialog() {
		createDialogs();
		return mExitDialog;
	}

	public void showToast(String msg) {

		// TODO: Should get text from toast instead of last message.

		showToast(msg, Toast.LENGTH_SHORT);
	}

	public void showToast(String msg, int duration) {
		if (msg == null) {
			throw new NullPointerException("Toast to show what?");
		}
		switch (duration) {
		case Toast.LENGTH_LONG:
			duration = TOAST_LONG_DELAY;
			break;
		case Toast.LENGTH_SHORT:
			duration = TOAST_SHORT_DELAY;
			break;
		default:
			break;
		}
		if (mToast != null) {
			mToast.setText(msg);
		} else {
			mMainThreadHandler.removeCallbacks(cancelToast);
			mToast = Toast.makeText(mContext, msg, duration);
			mMainThreadHandler.postDelayed(cancelToast, duration);
			mToast.show();
		}
	}

	// TODO:Remove it.
	// void useless() {
	// mNotifyManager.notify();
	// mDialog.cancel();
	// }
}
